package com.rain.builder;

import com.rain.bean.FieldInfo;
import com.rain.bean.TableInfo;
import com.rain.utils.Constants;
import com.rain.utils.DateUtils;
import com.rain.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class BuildQuery {

    private static final Logger logger = LoggerFactory.getLogger(BuildQuery.class);

    public static void execute(TableInfo tableInfo) throws IOException {
        File folder = new File(Constants.PACKAGE_QUERY_PATH);
        if(!folder.exists()){
            folder.mkdirs();
        }

        String className = tableInfo.getBeanName() + Constants.SUFFIX_BEAN_PARAM;
        File file = new File(folder, className + ".java");

        try{
            file.createNewFile();
        }catch(IOException e){
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedWriter bufferedWriter = null;

        try{
            outputStream = new FileOutputStream(file);
            outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
            bufferedWriter = new BufferedWriter(outputStreamWriter);
            // 基本模版
            bufferedWriter.write("package " + Constants.PACKAGE_QUERY + ";");
            bufferedWriter.newLine();
            bufferedWriter.newLine();

            // 判断是否添加日期类
            if(tableInfo.getHaveDate() || tableInfo.getHaveDateTime()){
                bufferedWriter.write("import java.util.Date;");
                bufferedWriter.newLine();

            }

            // 判断是否添加精确小数类
            if(tableInfo.getHaveBigDecimal()){
                bufferedWriter.write("import java.math.BigDecimal;");
                bufferedWriter.newLine();
            }
            bufferedWriter.newLine();

            // 构建类注释
            BuildComment.createClassComment(bufferedWriter,tableInfo.getComment() + "_查询类");
            bufferedWriter.write("public class " + className + " extends BaseQuery {");
            bufferedWriter.newLine();
            bufferedWriter.newLine();

            // 属性
            for (FieldInfo fieldInfo: tableInfo.getFieldList()) {
                // 添加注释
                BuildComment.createFieldComment(bufferedWriter,fieldInfo.getComment());

                bufferedWriter.write("\tprivate " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";");
                bufferedWriter.newLine();
                bufferedWriter.newLine();

                // String 类型的参数 添加Query字段
                if(ArrayUtils.contains(Constants.SQL_STAING_TYPE,fieldInfo.getSqlType())){
                    String properName = fieldInfo.getPropertyName() + Constants.QUERY_FUZZY;
                    bufferedWriter.write("\tprivate " + fieldInfo.getJavaType() + " " + properName + ";");
                    bufferedWriter.newLine();
                    bufferedWriter.newLine();
                }

                // Date 类型的参数 添加Query字段
                if(ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPES,fieldInfo.getSqlType()) || ArrayUtils.contains(Constants.SQL_DATE_TYPES,fieldInfo.getSqlType())){
                    String properNameStart = fieldInfo.getPropertyName() + Constants.QUERY_TIME_START;
                    String properNameEnd = fieldInfo.getPropertyName() + Constants.QUERY_TIME_END;

                    bufferedWriter.write("\tprivate " + "String" + " " + properNameStart + ";");
                    bufferedWriter.newLine();
                    bufferedWriter.newLine();

                    bufferedWriter.write("\tprivate " + "String" + " " + properNameEnd + ";");
                    bufferedWriter.newLine();
                    bufferedWriter.newLine();
                }
            }

            List<FieldInfo> fieldInfoList = tableInfo.getFieldList();
            //fieldInfoList.addAll(extendList);
            // xml生成时需要用到fieldInfoList  添加的extendList不需要
            // 生成两次解决fieldInfoList多余属性  extendList还不缺失
            buildGetSet(fieldInfoList,bufferedWriter);
            buildGetSet(tableInfo.getFieldExtendList(),bufferedWriter);


            bufferedWriter.write("}");
            bufferedWriter.flush();
        }catch (Exception e){
            logger.info("创建po失败", e.getMessage());
            e.printStackTrace();
        }finally {
            if(bufferedWriter != null) {
                bufferedWriter.close();
            }
            if(outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            if(outputStream != null) {
                outputStream.close();
            }
        }



    }

    private static void buildGetSet(List<FieldInfo> fieldInfoList, BufferedWriter bufferedWriter) throws IOException {
        // get set 方法
        for (FieldInfo fieldInfo: fieldInfoList) {
            String tempField = StringUtils.uperCaseFirstLetter(fieldInfo.getPropertyName());
            bufferedWriter.write("\tpublic void set" + tempField + "(" + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ")  { ");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\tthis." + fieldInfo.getPropertyName() +" = " + fieldInfo.getPropertyName() + ";");
            bufferedWriter.newLine();
            bufferedWriter.write("\t}");
            bufferedWriter.newLine();
            bufferedWriter.newLine();


            bufferedWriter.write("\tpublic " + fieldInfo.getJavaType() + " get" + tempField + "( ) { ");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\treturn this." + fieldInfo.getPropertyName() + ";");
            bufferedWriter.newLine();
            bufferedWriter.write("\t}");
            bufferedWriter.newLine();
            bufferedWriter.newLine();
        }
    }
}
